﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using System.IO;
using Jy123.Common;
using Jy123.BLL.Public;

namespace Jy123.Web.Manage
{
    public partial class AddSportType : MgConfig
    {
        protected string LoadInitial;
        protected string sportTypeName;
        protected string sportTypeDescription;
        protected string typeId;
        protected string typeName;
        protected string typeCount;
        protected string actionType = "Add";
        private const string strClub = "俱乐部";
        private const string strVenues = "赛场";
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("SportType.aspx");
            string strAt = Request.QueryString["at"];
            if (strAt != null)
            {
                if (strAt.Equals("Save"))
                {
                    Save();
                }
                else if (strAt.Equals("Modify"))
                {
                    Modify();
                }
            }
            LoadInitialValue();
        }

        protected void LoadInitialValue()
        {
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            List<Jy123.Model.Public_SportType> listSportType = BLLSportType.GetModelList(string.Empty);
            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("<table class='tblist'><tr><th>运动类型</th><th>比赛时间</th><th>参赛人数</th><th>图标</th><th></th></tr>");
            for (int i = 0; i < listSportType.Count; i++)
            {
                Jy123.Model.Public_SportType sportType = listSportType[i];
                int id = sportType.ID;
                string strTypeName = sportType.SName;
                int raceTime = sportType.RaceTime;
                strHtml.AppendFormat("<tr><td><input type='hidden' id='hidden{0}' name='hidden{0}' value='{0}' /><input type='hidden' id='typeHidden{0}' name='typeHidden{0}' value='{1}' />{1}</td><td>{2}</td><td>{3}</td>", i, strTypeName, raceTime.ToString(),sportType.Quantity.Value.ToString());
                strHtml.AppendFormat("<td><img alt = '' src= '{0}' class='listImg'> </td>", sportType.Logo);
                strHtml.AppendFormat("<td><input class='button'type ='submit' causesvalidation='false' onclick = 'Modify({0})' value='修改' /></td></tr>", id);
            }
            strHtml.Append("</table>");
            LoadInitial = strHtml.ToString();
            typeCount = listSportType.Count.ToString();
        }

        protected void Save()
        {
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            Jy123.BLL.Public_JType BLLJtype = new Jy123.BLL.Public_JType();

            string strType = Request.Form["hiddenType"].ToString();
            string strSportType = Request.Form["txtSportType"].ToString();
            string strDescription = Request.Form["areaDescription"].ToString();
            if (strType.Equals("Add"))
            {
                Jy123.Model.Public_SportType sportType = new Jy123.Model.Public_SportType();
                sportType.SName = strSportType;
                sportType.Description = strDescription;
                int sportTypeId = BLLSportType.Add(sportType);
                sportType.ID = sportTypeId;
                sportType.Logo = CreateLogo(sportTypeId);
                sportType.RaceTime = int.Parse(raceTimeTextBox.Text);
                sportType.Quantity = int.Parse(quantityTextBox.Text);
                BLLSportType.Update(sportType);

                //添加一个运动类型同时JType表里添加两条对应记录，分别是运动类型场地，运动类型俱乐部
                Jy123.Model.Public_JType jTypeClub = new Jy123.Model.Public_JType();
                Jy123.Model.Public_JType jTypeVenues = new Jy123.Model.Public_JType();
                jTypeVenues.JName = strSportType + strVenues;
                jTypeVenues.FSportType = sportTypeId;
                jTypeVenues.JFather = Convert.ToInt16(EnumConfig.JyType.Venues_Main);
                BLLJtype.Add(jTypeVenues);
                jTypeClub.JName = strSportType + strClub;
                jTypeClub.FSportType = sportTypeId;
                jTypeClub.JFather = Convert.ToInt16(EnumConfig.JyType.Club_Main);
                BLLJtype.Add(jTypeClub);

            }
            else if (strType.Equals("Update"))
            {
                string strId = Request.Form["hiddenId"].ToString();
                int id = int.Parse(strId);
                Jy123.Model.Public_SportType sportType = BLLSportType.GetModel(id);
                sportType.SName = strSportType;
                sportType.RaceTime = int.Parse(raceTimeTextBox.Text);
                sportType.Quantity = int.Parse(quantityTextBox.Text);
                sportType.Description = strDescription;
                if (fileLogo.PostedFile != null)
                {
                    string strLogo = CreateLogo(id);
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        sportType.Logo = strLogo;
                    }
                }


                //修改一个运动类型同时JType表里修改两条对应记录
                string strWhere = string.Format("FSportType = {0}", id);
                List<Jy123.Model.Public_JType> listJType = BLLJtype.GetModelList(strWhere.ToString());
                listJType[0].JName = strSportType + strVenues;
                listJType[1].JName = strSportType + strClub;
                BLLSportType.Update(sportType);
                BLLJtype.Update(listJType[0]);
                BLLJtype.Update(listJType[1]);
            }
            actionType = "Add";
            areaDescription.Value = string.Empty;
        }

        protected void Modify()
        {
            int id = int.Parse(Request.Form["hiddenId"].ToString());
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            Jy123.Model.Public_SportType sportType = BLLSportType.GetModel(id);
            sportTypeName = sportType.SName;
            quantityTextBox.Text = sportType.Quantity.Value.ToString();
            areaDescription.Value = sportType.Description;
            actionType = "Update";
            typeId = id.ToString();
            typeName = sportTypeName;
            raceTimeTextBox.Text = sportType.RaceTime.ToString();
            //ScriptManager.RegisterStartupScript(this.Form, typeof(Page), "Validate", "Validate()", true);
        }

        protected string CreateLogo(int sportTypeId)
        {
            CreateDir createDir = new CreateDir();
            HttpPostedFile postFile = fileLogo.PostedFile;
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateIconPath(CreateDir.SPORTTYPE, sportTypeId, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
